package com.mengwen.backstage.controller;

import com.mengwen.backstage.config.WeChatConfig;
import com.mengwen.backstage.pojo.*;
import com.mengwen.backstage.service.InvitationService;
import com.mengwen.backstage.service.PartnerService;
import com.mengwen.backstage.util.GetLastAccessTokenUtil;
import com.mengwen.backstage.util.JsSdkUtil;
import com.mengwen.backstage.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author LiangYonghui
 * @date 2020/3/30 18:59
 * @description 测试用的controller
 */
@Controller
@Slf4j
public class TestController {
    @Autowired
    WeChatConfig weChatConfig;
    @Autowired
    GetLastAccessTokenUtil getLastAccessTokenUtil;

    @Autowired
    PartnerService partnerService;

    @Autowired
    InvitationService invitationService;




    /**
     * 根据被邀请人本次发布问卷的金额的0.03更新
     * @param updateBalanceDto
     */
    @ResponseBody
    @PostMapping("test/partner/publish")  //0.03  0.02
    public void publishQuestion(@RequestBody UpdateBalanceDto updateBalanceDto) {
        String unionid = "1";
//        String token = request.getHeader("token");
//        Claims claims = JwtUtil.parseJWT(token);
//        String unionid = (String) claims.get("unionid");
//        String type = (String) claims.get("type");


        // 校验权限pc端的token，看是否具有权限
//        if (type.equals("type") && !(null == unionid)) {
            // 根据被邀请者的unionid查询邀请者的unionid
            String inviterUnionid = invitationService.findInviterUnionidByInviteeUnionid(unionid);
            if (inviterUnionid == null) {
                log.info("被邀请人：" + unionid + "未找到对应的邀请人");
                return;
            } else {
                System.out.println(updateBalanceDto);
                // 更新金额：0.03利润
                Double money = updateBalanceDto.getMoney();

                if ((money > 500) || (money < 0) || (money == null)) {
                    log.info("金额不符合规范，金额可能遭篡改");
                }

                log.info("金额1：" + money);

                money = money * ProfitRate.ZeroPointZeroThree;

                log.info("金额：" + money);

                log.info("inviterUnionid:"+ inviterUnionid);

                Integer count = partnerService.updateMoneyByunionid(inviterUnionid, money);

                if (count != 1) {
                    log.info("提现更新金额失败：unionid：" + inviterUnionid);
                } else {
                    log.info("提现金额更新成功，现执行redis排行榜数据更新操作");
                }


            }
        }





        @RequestMapping("/test_jssdk")
    public String testJsSdk(HttpServletRequest request, Model model) throws Exception {
        String appid = weChatConfig.getAppId();
        AccessToken accessToken = getLastAccessTokenUtil.getAccessToken();
        String url = request.getRequestURI();

        System.out.println(url);

        url = "http://mengwen.natapp1.cc" + url;
        System.out.println(url);

        Map<String, String> sign = JsSdkUtil.sign(url, accessToken.getToken(), appid);
        String jsapi_ticket = sign.get("jsapi_ticket");
        String nonceStr = sign.get("nonceStr");
        String signature = sign.get("signature");
        String timestamp = sign.get("timestamp");
        model.addAttribute("jsapi_ticket",jsapi_ticket);
        model.addAttribute("nonceStr",nonceStr);
        model.addAttribute("timestamp",timestamp);
        model.addAttribute("appid",appid);
        model.addAttribute("signature",signature);
        System.out.println(model.toString());
        return "test_share";
    }



    /**
     *  .claim("adminHeadimgurl", admin.getHeadimgurl())
     *                 .claim("adminUnionid", admin.getUnionid())
     *                 .claim("adminNickname", admin.getNickname())
     *                 .claim("adminOpenid", admin.getOpenid())
     *                 .claim("role","admin")
     * @param request
     * @return
     */

//    @RequestMapping("/to_check_question")
//    public String to_check_question(HttpServletRequest request,Model model){
//        String admin_token = request.getParameter("admin_token");
//        Claims claims = JwtUtil.checkJWT(admin_token);
//        String role = (String) claims.get("role");
//
//        if ("admin".equals(role)){
//            model.addAttribute("adminToken",admin_token);
//            log.info("to_check_question");
//            return "check_question";
//        }else {
//            return "login_door";
//        }
//
//
//    }
//    @RequestMapping("/to_now_data")
//    public String to_now_data(HttpServletRequest request,Model model){
//        String admin_token = request.getParameter("admin_token");
//        Claims claims = JwtUtil.checkJWT(admin_token);
//        String role = (String) claims.get("role");
//
//        if ("admin".equals(role)){
//            model.addAttribute("adminToken",admin_token);
//            log.info("进来to_now_data");
//            return "data";
//        }else {
//            return "login_door";
//        }
//
//
//    }
//    @RequestMapping("/to_help_publish")
//    public String to_help_publish(HttpServletRequest request,Model model){
//        String admin_token = request.getParameter("admin_token");
//        Claims claims = JwtUtil.checkJWT(admin_token);
//        String role = (String) claims.get("role");
//        if ("admin".equals(role)){
//            model.addAttribute("adminToken",admin_token);
//            log.info("进来to_help_publish");
//            return "create_question";
//        }else {
//            return "login_door";
//        }
//
//
//
//    }
//
//    @RequestMapping("/to_check_result")
//    public String to_check_result(){
//        return "check_result";
//    }
//    @RequestMapping("/admin/return_management_page")
//    public String return_management_page(HttpServletRequest request,Model model){
//        String admin_token = request.getParameter("admin_token");
//        System.out.println(admin_token);
//        Claims claims = JwtUtil.checkJWT(admin_token);
//        String role = (String) claims.get("role");
//
//        if ("admin".equals(role)) {
//            model.addAttribute("adminToken", admin_token);
//            return "back_management_page";
//        }else {
//            return "login_door";
//        }
//    }
//
//
//
//


}
